package com.bocloud.cmp.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.bocloud.cmp.dao.CloudVendorDao;
import com.bocloud.cmp.entity.resource.CloudVendor;
import com.bocloud.common.enums.BaseStatus;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.SimpleBean;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.JdbcGenericDao;
import com.bocloud.database.utils.SQLHelper;

/**
 * 
 * 云供应商Dao实现类
 * 
 * @author DZG
 * @since V1.0 2016年9月5日
 */
@Repository("cloudVendorDao")
public class CloudVendorDaoImpl extends JdbcGenericDao<CloudVendor, Long> implements CloudVendorDao {

	@Override
	public boolean delete(Long id, Long userId) throws Exception {
		String sql = "update cloud_vendor set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		return this.execute(sql, params) > 0;
	}

	@Override
	public boolean lock(Long id, Long userId) throws Exception {
		String sql = "update cloud_vendor set is_locked = 1, status = :status, gmt_modify = :gmtModify, mender_id = :menderId where id = :id and is_deleted = 0";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		params.put("status", BaseStatus.ABNORMAL.name());
		return this.execute(sql, params) > 0;
	}

	@Override
	public boolean active(Long id, Long userId) throws Exception {
		String sql = "update cloud_vendor set is_locked = 0, status = :status, gmt_modify = :gmtModify, mender_id = :menderId  where id = :id and is_deleted = 0";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		params.put("status", BaseStatus.NORMAL.name());
		return this.execute(sql, params) > 0;
	}

	@Override
	public CloudVendor getByName(String name) throws Exception {
		String sql = "select * from cloud_vendor where name = :name and is_deleted = 0";
		Map<String, Object> params = MapTools.simpleMap("name", name);
		List<CloudVendor> list = this.list(CloudVendor.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public List<CloudVendor> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.* from cloud_vendor a where a.is_deleted = 0 and a.type is not null";
		sql = SQLHelper.buildRawSql(sql, page, rows, params, sorter, "a");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		return this.list(CloudVendor.class, sql, paramMap);
	}

	@Override
	public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.id,a.name from cloud_vendor a where a.is_deleted = 0";
		sql = SQLHelper.buildSql(sql, 1, Integer.MAX_VALUE, params, sorter, "a");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		List<CloudVendor> rooms = this.list(CloudVendor.class, sql, paramMap);
		List<SimpleBean> beans = new ArrayList<>();
		for (CloudVendor room : rooms) {
			beans.add(new SimpleBean(room.getId(), room.getName()));
		}
		return beans;
	}

	@Override
	public int count(List<Param> params) throws Exception {
		String sql = "select count(1) from cloud_vendor a where a.is_deleted = 0 and a.type is not null";
		sql = SQLHelper.buildSql(sql, params, null, "a");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		return this.countQuery(sql, paramMap).intValue();
	}

	@Override
	public CloudVendor query(Long id) throws Exception {
		String sql = "select a.* from cloud_vendor a where a.id =:id";
		Map<String, Object> paramMap = MapTools.simpleMap("id", id);
		List<CloudVendor> list = this.list(CloudVendor.class, sql, paramMap);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public List<CloudVendor> listAll() throws Exception {
		String sql = "select * from cloud_vendor where is_deleted = 0";
		return this.list(CloudVendor.class, sql);
	}

	@Override
	public CloudVendor getByAddress(String address) throws Exception {
		String sql = "select * from cloud_vendor where address like :address and is_deleted = 0";
		Map<String, Object> params = MapTools.simpleMap("address", "%" + address + "%");
		List<CloudVendor> list = this.list(CloudVendor.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}
}
